﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using Microsoft.LightSwitch.Threading;
using System.Windows.Browser;

namespace LightSwitchApplication
{
    public partial class VisitDetail
    {
        //partial void Visit_Loaded(bool succeeded)
        //{
        //    // Write your code here.
        //    this.SetDisplayNameFromEntity(this.Visit);
        //}
        //partial void Visit_Changed()
        //{
        //    // Write your code here.
        //    this.SetDisplayNameFromEntity(this.Visit);
        //}
        //partial void VisitDetail_Saved()
        //{
        //    // Write your code here.
        //    this.SetDisplayNameFromEntity(this.Visit);
        //}


        partial void VisitDetail_Activated()
        {
            this.DisplayName = this.Visit.Id.ToString() + " (Визит)";
            try
            {
                this.FindControl("Save").IsEnabled = (this.Application.User.HasPermission(Permissions.CanEditVisit)
                        || this.Application.User.HasPermission(Permissions.CanManageMyVisit)
                    );
            }
            catch (Exception)
            {
            }
        }

        partial void VisitDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.Visit.VisitGoal != null && this.Visit.VisitGoal.VisitType != null)
            {
                this.SelectedVisitType = this.Visit.VisitGoal.VisitType;
            }
            this.FindControl("Save").IsEnabled = (this.Application.User.HasPermission(Permissions.CanEditVisit)
                    || this.Application.User.HasPermission(Permissions.CanManageMyVisit)
                );
        }

        partial void DownloadAppointment_Execute()
        {
            string url = String.Format(@"DownloadFile.aspx?VisitId={0}&VisitTimeFrom={1}&VisitTimeTo={2}&ShopName={3}&ShopFullAddress={4}&ShopContactEmail={5}&SalesPersonName={6}&SalesPersonContactEmail={7}&VisitDescription={8}",
                this.Visit.Id.ToString()
                , Utils.SimpleEncoder(this.Visit.VisitTimeFrom.ToString("yyyyMMddThhmm00"))
                , Utils.SimpleEncoder(this.Visit.VisitTimeTo != null ? ((DateTime)this.Visit.VisitTimeTo).ToString("yyyyMMddThhmm00") : "")
                , Utils.SimpleEncoder(this.Visit.VisitShop.ShopName)
                , Utils.SimpleEncoder(this.Visit.VisitShop.ShopAddresses.Count() > 0 ? this.Visit.VisitShop.ShopAddressFullName : "")
                , Utils.SimpleEncoder(this.Visit.VisitShop.ShopContacts.Count() > 0 ? this.Visit.VisitShop.ShopContacts.FirstOrDefault().ContactEmail : "")
                , Utils.SimpleEncoder(this.Visit.VisitSalesPerson != null ? this.Visit.VisitSalesPerson.SalesPersonName : "")
                , Utils.SimpleEncoder(this.Visit.VisitSalesPerson != null && this.Visit.VisitSalesPerson.Contacts.Count() > 0 ? this.Visit.VisitSalesPerson.Contacts.FirstOrDefault().ContactEmail : "")
                , Utils.SimpleEncoder(this.Visit.VisitDescription)
            );

            Dispatchers.Main.Invoke(() =>
            {
                HtmlPage.Window.Navigate(new Uri(url, UriKind.Relative), "_new");
            });
        }

        partial void ShowAuditHistory_Execute()
        {
            this.Application.ShowAuditLogHistory("Визиты", this.Visit.Id);
        }

        partial void VisitDetail_Closing(ref bool cancel)
        {
            if  (!(this.Application.User.HasPermission(Permissions.CanEditVisit)
                        || this.Application.User.HasPermission(Permissions.CanManageMyVisit)))
            {
                cancel = true;
            }
        }
    }
}
